<template>
  <div class="roleContainer">
    <el-dialog
      title="分配角色"
      :visible="dialogVisible"
      width="80%"
      @close="btnCancel"
    >
      <el-checkbox-group v-model="checkList">
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-row type="flex" justify="center">
          <el-col :span="8">
            <el-button size="mini" type="primary" @click="btnOK">提 交</el-button>
            <el-button size="mini" @click="btnCancel">取 消</el-button>
          </el-col>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, assignRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'

export default {
  name: '',
  components: {},
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getRoleList()
  },
  methods: {
    async btnOK() {
      console.log(this.userId)
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('提交成功')
      this.btnCancel()
    },
    btnCancel() {
      this.roleIds = [] // 清空之前的值
      this.$emit('update:dialogVisible', false)
    },
    async getRoleList() {
      const { rows } = await getRoleList()
      // console.log(res)
      this.list = rows
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.checkList = roleIds
    }
  }
}
</script>

<style scoped>

</style>
